package com.qf;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class TestApp {
    public static void main(String[] args) throws InterruptedException {
        //基于JJWT生成一个jwt字符串 (头，载荷，签名--密钥)
        Map<String, Object> map = new HashMap<>();
        map.put("username","zs");
        map.put("roles","P1,P2");
        String jwt = Jwts.builder()
                //这个设置我们自己数据的方法一定要放在最前面
                .setClaims(map)
                .setSubject("zhangsan")
                .setExpiration(new Date(System.currentTimeMillis() + 1000))
                .signWith(SignatureAlgorithm.HS256, "qf666666")
                .compact();

        System.out.println(jwt);
        System.out.println("======================");
        Thread.sleep(1500);

        //如果jwt过期，抛出ExpiredJwtException异常
        Object map2 = Jwts.parser().setSigningKey("qf666666").parse(jwt).getBody();
        System.out.println(map2);

    }
}
